package trying.ocr;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class ScanPhoto {


//        public String reg(@RequestParam("file")MultipartFile file) throws IOException {
        public static void main(String[] args) throws IOException {
            String result = "";
            String dir = "D:\\test";
            File dirs = new File(dir);
            File[] files = dirs.listFiles();

            for(File file : files) {
//            file.transferTo(save);
                String cmd = String.format("tesseract %s stdout", dir + "\\" + file.getName(), "chi_sim");
                result = cmd(cmd);
            }
        }

        public static String cmd(String cmd) {
            BufferedReader br = null;
            try {
                Process p = Runtime.getRuntime().exec(cmd);
                br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line = null;
                StringBuilder sb = new StringBuilder();
                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                }
                return sb.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            finally
            {
                if (br != null)
                {
                    try {
                        br.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            return null;
        }
}
